﻿using System;
using System.Collections.Generic;
using System.Linq;
using Enyim.Caching.Memcached;
using Newtonsoft.Json.Linq;
using Wicture.DbRESTFul;
using Wicture.DbRESTFul.SQL;
using Wicture.QPZS.EPC.Models;

namespace Wicture.QPZS.EPC.Repository
{
    public class MaintainBookRepository : DbRESTFulRepository
    {
        public MaintainBookRepository() : base()
        {
            List<DbRoute> dbConfigs = ConfigurationManager.Settings.GetConfig<List<DbRoute>>("DbRoutes");
            var connectionStr = dbConfigs.FirstOrDefault(t => t.Name == "MaintainManual")?.ReadConnectionString;
            if (string.IsNullOrEmpty(connectionStr))
            {
                throw new LogicalException("maintain manual db config should be configed");
            }
            base.ConnectionManager.ReadConnectionString = connectionStr;
        }
        public object ListModelCatalog(JToken param)
        {
            List<ModelCatalog> allCatalogs = base.Invoke<ModelCatalog>("List_Model_Catalog", new { });
            List<ModelCatalogDto> dtos = new List<ModelCatalogDto>();
            ProcessModelCatalog(dtos, allCatalogs);
            return dtos;
        }

        public object ListBookCatalog(JToken param)
        {
            List<ManualCatalog> allCatalogs = base.Invoke<ManualCatalog>("List_Manual_Catalog", param);
            List<ManualCatalogDto> dtos = new List<ManualCatalogDto>();
            ProcessManualCatalog(dtos, allCatalogs);
            return dtos;
        }

        public object ListCatalogPictures(JToken param)
        {
            param["origin"] = "https://cdn.91autoparts.com/";
            List<ManualPicture> data = base.Invoke<ManualPicture>("List_Catalog_Picture", param);
            foreach (var pic in data)
            {
                var tt = pic.originName.Split(".");
                var t1 = tt.Take(tt.Length - 1);
                pic.originName = string.Join("", t1);
            }
            return data;
        }

        public object GetModelConfig(JToken param)
        {
            return base.Invoke("Get_Model_Config", param);
        }

        private void ProcessModelCatalog(List<ModelCatalogDto> ret, List<ModelCatalog> catalogs, int level = 1, int parentId = 0)
        {
            List<ModelCatalog> nodesToAdd = parentId != 0 ? catalogs.Where(t => t.level == level && t.parentId == parentId).ToList() : catalogs.Where(t => t.level == level).ToList();
            if (nodesToAdd.Count > 0)
            {
                foreach (var node in nodesToAdd)
                {
                    
                    var toAdd = new ModelCatalogDto
                    {
                        id = node.id,
                        identity = node.identity,
                        name = node.name,
                        modelId = node.modelId,
                        iconUrl = node.iconUrl,
                        pinYinCapital = node.pinYinCapital,
                        manualType = node.manualType,
                        entry = node.entry,
                        subCatalogs = new List<ModelCatalogDto>()
                    };
                    if (node.level != 3 || (node.level == 3 && node.modelId != 0))
                    {
                        ret.Add(toAdd);
                    }
                    ProcessModelCatalog(toAdd.subCatalogs, catalogs, level + 1, node.id); 
                }
                ret.Sort();
            }
        }

        private void ProcessManualCatalog(List<ManualCatalogDto> ret, List<ManualCatalog> catalogs, int level = 1, int parentId = 0)
        {
            List<ManualCatalog> nodesToAdd = parentId != 0 ? catalogs.Where(t => t.level == level && t.parentId == parentId).ToList() : catalogs.Where(t => t.level == level).ToList();
            if (nodesToAdd.Count > 0)
            {
                foreach (var node in nodesToAdd)
                {
                    var toAdd = new ManualCatalogDto()
                    {
                        id = node.id,
                        identity = node.identity,
                        name = node.name,
                        subCatalogs = new List<ManualCatalogDto>()
                    };
                    ret.Add(toAdd);
                    ProcessManualCatalog(toAdd.subCatalogs, catalogs, level + 1, node.id);
                }
                ret.Sort();
            }
        }
    }
}
